fix: Make XML parsing more robust with regex fallback (#455)#473
Open
vivekvar-dl wants to merge 1 commit intoSylphAI-Inc:mainfrom
Open
fix: Make XML parsing more robust with regex fallback (#455)#473vivekvar-dl wants to merge 1 commit intoSylphAI-Inc:mainfrom
vivekvar-dl wants to merge 1 commit intoSylphAI-Inc:mainfrom
Conversation
- Add XML sanitization to remove invalid control characters - Handle CDATA sections properly by extracting content - Implement regex-based fallback when ET.fromstring fails - Improve text extraction to handle nested XML elements - Add comprehensive error recovery for malformed LLM output This addresses issue SylphAI-Inc#455 where XML parsing was failing on malformed output from Gemini Flash 2.5. The parser now gracefully falls back to regex extraction when strict XML parsing fails, ensuring that the proposed_variable and other fields are still extracted correctly. The changes include: 1. _sanitize_xml() method to clean invalid characters and CDATA 2. _extract_with_regex() fallback parser using regex patterns 3. Enhanced get_element_text() to handle nested elements 4. Better error handling with informative logging Tested with various malformed inputs including unclosed tags, invalid characters, CDATA sections, and completely broken XML.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR addresses issue #455 by making XML parsing in the TGD optimizer more robust when handling malformed output from LLMs like Gemini Flash 2.5.
Problem
The current XML parser (
CustomizedXMLParserintgd_optimizer.py) fails completely when LLMs produce malformed XML, leading to loss of theproposed_variableand other important fields. Users reported this was more frequent with Gemini Flash 2.5.Solution
This PR implements a multi-layered approach to XML parsing:
ET.fromstringfails, falls back to regex-based extractionChanges
_sanitize_xml()method to clean invalid characters and extract CDATA content_extract_with_regex()fallback parser using regex patternsget_element_text()helper to handle nested elements properlylog.warning()andlog.info()Testing
Tested with various malformed inputs:
All test cases now successfully extract the
proposed_variablefield.Backward Compatibility
✅ Fully backward compatible - well-formed XML still parses via the standard XML parser. The regex fallback only activates when XML parsing fails.
Fixes #455